/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import tpa.TablaPeriodica.Elemento;
import tpa.TablaPeriodica.SubClase;

/**
 *
 * @author leandro
 */
public class AgregarElementoAction extends TablaPeriodicaBaseAction {
    
    /* forward name="success" path="" */
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        try {
            AgregarElementoActionForm actionForm = (AgregarElementoActionForm)form;
            actionForm.setStatus("");

            String propiedadesString = "Propiedades (" + actionForm.getSelectedProperties().size() + ")";
            for(Long currentPropiedad : actionForm.getSelectedProperties().values())
            {
                SubClase subclase = this.getTablaPeriodicaSessionBean().ObtenerSubClase(currentPropiedad);
                if(subclase != null)
                    propiedadesString += "<br/>" + subclase.toString();
            }


            Elemento elemento = this.getTablaPeriodicaSessionBean().AgregarElemento(actionForm.getNumeroAtomico(),
                                                actionForm.getNombre(),
                                                actionForm.getSimbolo(),
                                                actionForm.getPesoAtomico(),
                                                actionForm.getImageUrl(),
                                                actionForm.getInfoUrl(),
                                                actionForm.getSelectedProperties().values());

            if(actionForm.getNumeroAtomico() <= 0)
                actionForm.setStatus("Elemento '" + elemento.toString() + "' agregado correctamente");
            else
                actionForm.setStatus("Elemento '" + elemento.toString() + "' actualizado correctamente");

            actionForm.setStatus(propiedadesString);

            return mapping.findForward(SUCCESS);
        }
        catch (Exception exception)
        {
            ActionErrors errors = new ActionErrors();

            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.exception",
                    (exception.getCause() == null ? exception.getMessage() : exception.getCause().getMessage()
                    )));

            saveErrors(request, errors);
            return mapping.findForward(ERROR);
        }
    }
}
